<?php
/**
 *
 * Author: LiuJianPeng
 * Date: 2023/8/7
 * Time: 16:19
 */

namespace App\Service\Core\Sys\Area;

use App\Model\Core\Sys\SysAreaModel;
use App\Service\Core\CoreBaseService;
use App\Traits\ModelStoreTrait;
use Illuminate\Support\Arr;

/**
 * Class DriverStoreService
 * @method  $this setParentId($val)
 * @method  $this setAreaCode($val)
 * @method  $this setAreaName($val)
 * @method  $this setSimpleSpelling($val)
 * @method  $this setSortCode($val)
 * @method  $this setStatus($val)
 * @method  $this setRemark($val)
 */
class SysAreaStoreService extends CoreBaseService
{
    use ModelStoreTrait;

    private array $required_param = ['area_code', 'area_name', 'simple_spelling', 'sort_code'];

    /**
     * 新建OR修改地区信息
     * @param int $area_id
     * @return array
     * @throws \Throwable
     */
    public function store(int $area_id = 0): array
    {
        //接收验证参数
        $this->check_required_param($this->required_param);
        if (!empty($this->errors)) return alert_info(1, $this->errors[0], $this->errors);

        //过滤参数
        $data_param = ['parent_id', 'area_code', 'area_name', 'simple_spelling', 'sort_code', 'status', 'remark'];
        $other_param = [];
        $all_param = Arr::collapse([$data_param, $other_param]);
        foreach ($this->params as $key => $value) {
            if (easy_in_array($key, $all_param)) $$key = $value;
        }

        //初始data数据
        $data = [];
        foreach ($data_param as $value) {
            if (easy_in_array($value, $all_param)) $data[$value] = isset($$value) ? $$value : '';
        }

        //验证信息
        if (!empty($parent_id) && empty($parent_area = SysAreaModel::getInstance()->getOne($parent_id))) return alert_info(1, '上级地区信息不存在');

        //开启事务
        $connection = SysAreaModel::getInstance()->getConnection();
        $connection->beginTransaction();
        if ($area_id) {
            //判断数据
            if (empty(SysAreaModel::getInstance()->getOne($area_id))) {
                $connection->rollBack();
                return alert_info(1, '修改信息不存在');
            }

            //更新
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $res = SysAreaModel::getInstance()->updateOne($area_id, $data);
            $this->addLog('行政区域', '修改行政区域信息【' . $data['area_name'] . '】', $this->operate_id, $this->operate_by);
        } else {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['created_by'] = $this->operate_by;
            $data['layer'] = !empty($parent_area['layer']) ? $parent_area['layer']++ : 1;
            $res = SysAreaModel::getInstance()->addOne($data);
            $area_id = $res;
            $this->addLog('行政区域', '添加行政区域信息【' . $data['area_name'] . '】', $this->operate_id, $this->operate_by);
        }

        if ($res === false) {
            $connection->rollBack();
            return alert_info(1, '操作失败：数据库繁忙');
        }

        //提交事务
        $connection->commit();
        return alert_info(0, '操作成功', ['id' => $area_id]);
    }
}
